//
//  HomeTableViewCell.swift
//  ZHTQWeibo
//
//  Created by ZHTQ on 2018/9/19.
//  Copyright © 2018年 ZHTQ. All rights reserved.
//

import UIKit

class HomeTableViewCell: UITableViewCell {
    /// 作者头像
    @IBOutlet weak var iconImageView: UIImageView!
    /// 微博正文
    @IBOutlet weak var contentLable: UILabel!
    /// 微博时间
    @IBOutlet weak var timeLable: UILabel!
    /// 微博来源
    @IBOutlet weak var fromLable: UILabel!
    /// 微博昵称
    @IBOutlet weak var nameLable: UILabel!
    /// 微博vip
    @IBOutlet weak var vipImageView: UIImageView!
    /// 微博认证
    @IBOutlet weak var verifiedImageView: UIImageView!
    
    /// colletionVIew
    @IBOutlet weak var contentCollectionView: HomeCollectionView!
    /// 底部视图
    @IBOutlet weak var bottonView: UIView!
    /// 转发微博文字
    @IBOutlet weak var forearTextLable: UILabel!
    
    
    /// 微博数据
    var statusViewModel: StatusViewModel? {
        didSet{
            // 头像
            if let imageURL = statusViewModel?.iconURL {
                iconImageView.sd_setImage(with: imageURL, completed: nil)
            }
            
            // 正文
            contentLable.text = statusViewModel?.status?.text
            
            // 昵称
            nameLable.text = statusViewModel?.status?.user?.screen_name
            
            // 来源
            fromLable.text = nil
            if let from = statusViewModel?.source_text {
                fromLable.text = from
            }
            
            // 时间 Thu Sep 20 20:04:54 +0800 2018
            if let timeStr = statusViewModel?.created_time {
                // 设置字符串微博时间
                timeLable.text = timeStr
            }
            
            // vip
            vipImageView.image = nil
            nameLable.textColor = UIColor.black
            if let vip = statusViewModel?.mbrank_image {
                vipImageView.image = vip
                nameLable.textColor = UIColor.orange
            }
            
            // 认证
            verifiedImageView.image = nil
            if let verfield = statusViewModel?.verified_image {
            verifiedImageView.image = verfield
            }
            
            if let text = statusViewModel?.forwoardText {
                
                forearTextLable.preferredMaxLayoutWidth = UIScreen.main.bounds.width - 2 * 10.0
                forearTextLable.text = text
            }
            
            // 给collectionView赋值模型数据
            contentCollectionView.statusViewModel = statusViewModel
            
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        //设置正文的最大宽度
        contentLable.preferredMaxLayoutWidth = UIScreen.main.bounds.width - 2 * 10.0
        
        
    }
    
    //MARK:- 外部控制方法
    /// 返回当前的cell的行高
    func cellHeight(statusViewModel: StatusViewModel) -> CGFloat{
        // 赋值数据
        self.statusViewModel = statusViewModel
        // 刷新cell
        layoutIfNeeded()
        
        // 计算行高
        let height = bottonView.frame.maxY
        
        return height
    }

}



